/**
 * 获取当前日期前后多少天的日期，之前多少天传正数，后面多少天传负数，今天传0，
 * num为传入的数字， time为传入的指定日期，如果time不传，则默认为当前时间
 */
export const getBeforeDate = (num, time) => {
  let n = num;
  let d = "";
  if (time) {
    d = new Date(time);
  } else {
    d = new Date();
  }
  let year = d.getFullYear();
  let mon = d.getMonth() + 1;
  let day = d.getDate();
  if (day <= n) {
    if (mon > 1) {
      mon = mon - 1;
    } else {
      year = year - 1;
      mon = 12;
    }
  }
  d.setDate(d.getDate() - n);
  year = d.getFullYear();
  mon = d.getMonth() + 1;
  day = d.getDate();
  let s = year + "-" + (mon < 10 ? "0" + mon : mon) + "-" + (day < 10 ? "0" + day : day);
  return s;
};

export const isNull = object => {
  if (object == null || typeof object == "undefined") {
    return true;
  }
  return false;
};

// 获取星期几
export const getWeek = dateString => {
  let date = null;
  if (isNull(dateString)) {
    date = new Date();
  } else {
    let dateArray = dateString.split("-");
    date = new Date(dateArray[0], parseInt(dateArray[1] - 1), dateArray[2]);
  }
  let weeks = new Array("日", "一", "二", "三", "四", "五", "六");
  return "星期" + weeks[date.getDay()];
};

// 获取昨天日期
export const getYesterday = () => {
  let today = new Date(); // 获取今天的日期
  let yesterday = new Date(today); // 通过复制今天的日期创建一个新的日期对象
  yesterday.setDate(today.getDate() - 1); // 将新日期对象的日期减去一天
  let year = yesterday.getFullYear(); // 获取年份
  let mon = yesterday.getMonth() + 1; // 获取月份（注意月份是从0开始计数的，因此要加1）
  let day = yesterday.getDate();
  let s = year + "-" + (mon < 10 ? "0" + mon : mon) + "-" + (day < 10 ? "0" + day : day);
  return s;
};
